package com.yiwannuofulasi.config;

import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 解决Nacos注册服务打成war包不展示问题
 */
@Component
public class NacosConfig implements ApplicationRunner {

    @Autowired(required = false)
    private NacosAutoServiceRegistration registration;

    @Value("${server.port}")
    Integer port;

    @Override
    public void run(ApplicationArguments args) {
        if (registration != null && port != null) {
            //如果getTomcatPort()端口获取异常,就采用配置文件中配置的端口
            Integer tomcatPort = port;
            registration.setPort(tomcatPort);
            registration.start();
        }
    }
}
